package com.wang.diaryauth.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.wang.diaryauth.model.vo.user.UserLoginVO;
import com.wang.diaryauth.service.UserService;
import com.wang.framework.aspect.ApiOperationLog;
import com.wang.framework.common.response.Response;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author : wangs
 * @date : 2024/10/28 9:52
 * @modyified By :
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/login")
    @ApiOperationLog(description = "用户登录/注册")
    public Response<String> loginAndRegister(@Validated @RequestBody UserLoginVO userLoginReqVO) {
        return userService.loginAndRegister(userLoginReqVO);
    }

    // 测试登录，浏览器访问： http://localhost:8080/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }
}
